package org.linlinjava.litemall.admin.service;

import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import me.chanjar.weixin.common.error.WxErrorException;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.bcrypt.MessageSubscribeUtil;
import org.linlinjava.litemall.db.dao.LitemallBookReviewMapper;
import org.linlinjava.litemall.db.domain.LitemallBookReview;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallBookReviewService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.vo.BookReviewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class AdminBookReviewService {
    @Autowired
    private LitemallBookReviewService bookReviewService;
    @Autowired
    private LitemallBookReviewMapper bookReviewMapper;
    @Autowired
    private LitemallUserService userService;
    @Autowired
    private MessageSubscribeUtil messageSubscribeUtil;


    /**
     * 根据不同状态（0：全部，1：待审核::2：已通过，3：未通过）查询list
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<BookReviewVo> queryList(Integer status, Integer page, Integer limit, String sort, String order) {
        return bookReviewService.queryList(status, page, limit, sort, order);
    }

    public Object pass(Integer bookReviewId) {
        LitemallBookReview bookReview = bookReviewMapper.selectByPrimaryKey(bookReviewId);
        if (bookReview == null) {
            return ResponseUtil.fail(500,"该条书评已被用户删除");
        }
        bookReview.setStatus(2);
        bookReviewMapper.updateByPrimaryKeySelective(bookReview);
        sendBookReviewPass(bookReviewId,true);
        return ResponseUtil.ok();
    }

    public Object fail(Integer bookReviewId) {
        LitemallBookReview bookReview = bookReviewMapper.selectByPrimaryKey(bookReviewId);
        if (bookReview == null) {
            return ResponseUtil.fail(500,"该条书评已被用户删除");
        }
        bookReview.setStatus(3);
        bookReviewMapper.updateByPrimaryKeySelective(bookReview);
        sendBookReviewPass(bookReviewId,false);
        return ResponseUtil.ok();
    }

    public void sendBookReviewPass(Integer bookReviewId,Boolean isPassed) {
        LitemallBookReview bookReview = bookReviewService.findById(bookReviewId);
        if (bookReview == null) {
            throw new RuntimeException("该评论不存在");
        }
        Integer userId = bookReview.getUserId();
        LitemallUser user = userService.findById(userId);
        String openid = user.getWeixinOpenid();
        String templateId = "1TIZypOhuBKXK3kEWGDSJ_uxWtaFeweU1Z3kwBS4xkk";
        String page = "pages/index/index";
        List<WxMaSubscribeMessage.Data> data = new ArrayList<>();

        WxMaSubscribeMessage.Data data1 = new WxMaSubscribeMessage.Data();
        data1.setName("thing1");
        data1.setValue(user.getNickname());
        data.add(data1);

        WxMaSubscribeMessage.Data data2 = new WxMaSubscribeMessage.Data();
        data2.setName("thing2");
        data2.setValue(bookReview.getContent());
        data.add(data2);

        WxMaSubscribeMessage.Data data3 = new WxMaSubscribeMessage.Data();
        data3.setName("phrase3");
        if (isPassed){
            data3.setValue("审核通过");
        }
        else {
            data3.setValue("审核未通过");
        }
        data.add(data3);

        try {
            messageSubscribeUtil.sendSubscribeMsg(openid,templateId,data,page);
        } catch (WxErrorException e) {
            e.printStackTrace();
            System.out.println(e);
        }

    }


}
